<?php

namespace App\Http\Controllers\Admin;

use App\Service\CertificateService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CertificateController extends Controller
{
    private $certificateService;

    public function __construct(CertificateService $certificateService)
    {
        $this->certificateService = $certificateService;
    }

    //获取所有已经审核的凭证
    public function getCertificateList(){
        $data = $this->certificateService->getCertificateList()->toArray();
//        dd($data);
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['certificate_content'] = json_decode($data['data'][$key]['certificate_content']);
        }
        return view('certificate_list')->with('data',$data);
    }

    public function addCertificate(){
        return view('certificate_add');
    }

    public function doAddCertificate(Request $request){
        $data = $request->all();
        $this->certificateService->addCertificate($data);
        return redirect('/getVerifyCertificateList');

    }

    public function getVerifyCertificateList(){
        $data = $this->certificateService->getVerifyCertificateList()->toArray();
        foreach ($data['data'] as $key => $value){
            $data['data'][$key]['certificate_content'] = json_decode($data['data'][$key]['certificate_content']);
        }
        return view('certificate_verify_list')->with('data',$data);
    }

    public function verifyCertificate(Request $request){
        $id = $request->get('id');
        $this->certificateService->verifyCertificate($id);
        return redirect('/getCertificateList');
    }

}
